#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QSystemTrayIcon>
#include <QtNetwork/QNetworkAccessManager>
#include <QNetworkReply>
#include "timespan.h"
#include <QList>

class QSettings;
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QMainWindow *parent = nullptr);
    ~MainWindow();
    QStringList getAutoConntectTime();

    void setAutoConntectTime(const QStringList &value);


public slots:
    void usernameChanged();
    void passwordChanged();
    void addTime();
    void deleteTimeSpan(int);
    void freshAutoConnectTime();
    void saveAutoConnectTime();
    void systemTrayIconQuitAction();
    void systemTrayIconShowAction();
    void startupChecked(bool);
    void autoLoginChecked(bool);
    void loginLogic();
    void logoutLogic();
    void slotReadyRead();
    void slotError(QNetworkReply::NetworkError);
    void slotSslErrors(QList<QSslError>);
    void unknownError();
    void speedChanged(int);

private:
    Ui::MainWindow *ui;
    QSettings *settings;
    QStringList autoConntectTime;
    bool autoLogin;
    bool startup;
    QSystemTrayIcon *systemTrayIcon;
    QNetworkAccessManager *manager;
    QNetworkReply *reply;
    enum State{
        IDLE,PREPARELOGIN,LOGINFAILED,LOGINSUCCESS,PREPARELOGOUT,LOGOUTFAILED,LOGOUTSUCCESS
    };
    int speedtype=0;
    int state=IDLE;
    int interval=0;
    void init();
    void setSpeedByType(int);
    int timeToInt(QTime time);
    bool checkCurrentRun(int);
    QList<TimeSpan> getTimeSpan(QStringList li);
    QList<TimeSpan> timeSpan;
    bool run=false;
    int lastLoginTime;
    int delay=-1;
    int timeout=10;
    int tocnt=-1;
protected:
    void changeEvent(QEvent *);
    void timerEvent(QTimerEvent*);

};

#endif // MAINWINDOW_H
